<?php
/**
 * Created by PhpStorm.
 * User: dingboxu
 * Date: 17/2/21
 * Time: 下午5:07
 */

use Workerman\Protocols\Http;
use Workerman\Worker;

require_once __DIR__ . '/../../Workerman/Autoloader.php';

require_once __DIR__.'/Config/Const.php';

// 创建一个Worker监听8080端口，使用http协议通讯
// $http_worker = new \Workerman\WebServer("http://0.0.0.0:8080");

$http_worker = new Worker("http://0.0.0.0:8088");

//$http_worker->addRoot('192.168.1.113', '/var/www/html/');

// 启动4个进程对外提供服务
$http_worker->count = 4;

// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function($connection,$data)
{

    $data_arr = json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true);
    print_r($data_arr);
    //$data_arr = $data['post'];

    $dataDictionary = $data_arr[DATA_DICTIONARY];

    if (array_key_exists(REQUEST_ID,$data_arr)) {
        $requestId = $data_arr[REQUEST_ID];
    } else {
        $requestId = 0;
    }

    \Workerman\Protocols\Http::header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    \Workerman\Protocols\Http::header("Access-Control-Allow-Credentials:true");

    Http::sessionStart();

    //print_r($_SESSION);
    switch($data_arr[SYS_EVENT]) {

        case 'TEST':
            // 测试
            new \ActionsHttp\Test('TEST',$requestId,$dataDictionary,$connection);
            break;
        case 'TEST1':
            // 测试
            new \ActionsHttp\Test1('TEST1',$requestId,$dataDictionary,$connection);
            break;

        /*************** 商户 ***************/
        case EVENT_RESTAURANT_LOGIN:
            // 商家登录
            new \ActionsHttp\RestaurantLogin(EVENT_RESTAURANT_LOGIN,$requestId,$dataDictionary,$connection);
            break;

        // 商品分类
        case EVENT_RESTAURANT_FOOD_GROUPS_ADD:
            // 添加商品分类
            new \ActionsHttp\RestaurantFoodGroupsAdd(EVENT_RESTAURANT_FOOD_GROUPS_ADD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_DELETE:
            // 删除商品分类
            new \ActionsHttp\RestaurantFoodGroupsDelete(EVENT_RESTAURANT_FOOD_GROUPS_DELETE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_UPDATE:
            // 更新商品分类
            new \ActionsHttp\RestaurantFoodGroupsUpdate(EVENT_RESTAURANT_FOOD_GROUPS_UPDATE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_GET_LIST:
            // 获取商品分类列表
            new \ActionsHttp\RestaurantFoodGroupsGetList(EVENT_RESTAURANT_FOOD_GROUPS_GET_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_GET_RECORD:
            // 获取单个商品分类
            new \ActionsHttp\RestaurantFoodGroupsGetRecord(EVENT_RESTAURANT_FOOD_GROUPS_GET_RECORD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GROUPS_UPDATE_ORDER:
            // 更新商品分类排序
            new \ActionsHttp\RestaurantFoodGroupsUpdateOrder(EVENT_RESTAURANT_FOOD_GROUPS_UPDATE_ORDER,$requestId,$dataDictionary,$connection);
            break;

        // 商品标签
        case EVENT_RESTAURANT_FOOD_TAGS_ADD:
            // 添加商品标签
            new \ActionsHttp\RestaurantFoodTagsAdd(EVENT_RESTAURANT_FOOD_TAGS_ADD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_TAGS_DELETE:
            // 删除商品标签
            new \ActionsHttp\RestaurantFoodTagsDelete(EVENT_RESTAURANT_FOOD_TAGS_DELETE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_TAGS_UPDATE:
            // 更新商品标签
            new \ActionsHttp\RestaurantFoodTagsUpdate(EVENT_RESTAURANT_FOOD_TAGS_UPDATE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_TAGS_GET_LIST:
            // 获取商品标签列表
            new \ActionsHttp\RestaurantFoodTagsGetList(EVENT_RESTAURANT_FOOD_TAGS_GET_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_TAGS_GET_RECORD:
            // 获取单个标签
            new \ActionsHttp\RestaurantFoodTagsGetRecord(EVENT_RESTAURANT_FOOD_TAGS_GET_RECORD,$requestId,$dataDictionary,$connection);
            break;

        // 商品
        case EVENT_RESTAURANT_FOOD_ADD:
            // 添加商品
            new \ActionsHttp\RestaurantFoodAdd(EVENT_RESTAURANT_FOOD_ADD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_DELETE:
            // 删除商品
            new \ActionsHttp\RestaurantFoodDelete(EVENT_RESTAURANT_FOOD_DELETE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_UPDATE:
            // 更新商品
            new \ActionsHttp\RestaurantFoodUpdate(EVENT_RESTAURANT_FOOD_UPDATE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GET_LIST:
            // 获取商品列表
            new \ActionsHttp\RestaurantFoodGetList(EVENT_RESTAURANT_FOOD_GET_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_GET_RECORD:
            // 获取单个商品信息
            new \ActionsHttp\RestaurantFoodGetRecord(EVENT_RESTAURANT_FOOD_GET_RECORD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_SELL_CHANGE:
            // 上架/下架商品
            new \ActionsHttp\RestaurantFoodSellChange(EVENT_RESTAURANT_FOOD_SELL_CHANGE,$requestId,$dataDictionary,$connection);
            break;

        //  ------  桌子管理  ------
        // 桌子
        case EVENT_TABLE_ADD:
            // 添加桌子
            new \ActionsHttp\TableAdd(EVENT_TABLE_ADD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_TABLE_UPDATE:
            // 更新桌子
            new \ActionsHttp\TableUpdate(EVENT_TABLE_UPDATE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_TABLE_GET_RECORD:
            // 获取单个桌子记录
            new \ActionsHttp\TableGetRecord(EVENT_TABLE_GET_RECORD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_TABLE_GET_LIST:
            // 获取所有桌子
            new \ActionsHttp\TableGetList(EVENT_TABLE_GET_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_TABLE_DELETE:
            // 删除桌子
            new \ActionsHttp\TableDelete(EVENT_TABLE_DELETE,$requestId,$dataDictionary,$connection);
            break;


        // -----  餐厅信息管理  -----
        case EVENT_RESTAURANT_INFO_GET:
            // 餐厅信息获取
            new \ActionsHttp\RestaurantInfoGet(EVENT_RESTAURANT_INFO_GET,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_INFO_GET_BASE:
            // 餐厅基本信息获取
            new \ActionsHttp\RestaurantInfoGetBase(EVENT_RESTAURANT_INFO_GET_BASE,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_INFO_ANNOUNCE_SET:
            // 编辑餐厅公告
            new \ActionsHttp\RestaurantInfoAnnounceSet(EVENT_RESTAURANT_INFO_ANNOUNCE_SET,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_INFO_TEL_SET:
            // 编辑餐厅电话
            new \ActionsHttp\RestaurantInfoTelSet(EVENT_RESTAURANT_INFO_TEL_SET,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_INFO_OPEN_HOURS_SET:
            // 编辑餐厅营业时间
            new \ActionsHttp\RestaurantInfoOpenHousSet(EVENT_RESTAURANT_INFO_OPEN_HOURS_SET,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_SETTINGS_PRINT_GET:
            // 打印机设置获取
            new \ActionsHttp\RestaurantSettingsPrintGet(EVENT_RESTAURANT_SETTINGS_PRINT_GET,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_SETTINGS_PRINT_SET:
            // 打印机设置
            new \ActionsHttp\RestaurantSettingsPrintSet(EVENT_RESTAURANT_SETTINGS_PRINT_SET,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_SETTINGS_RING_GET:
            // 提示音设置获取
            new \ActionsHttp\RestaurantSettingsRingGet(EVENT_RESTAURANT_SETTINGS_RING_GET,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_SETTINGS_RING_SET:
            // 提示音设置
            new \ActionsHttp\RestaurantSettingsRingSet(EVENT_RESTAURANT_SETTINGS_RING_SET,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_FOOD_UPDATE_ORDER:
            // 更新商品排序商品
            new \ActionsHttp\RestaurantFoodUpdateOrder(EVENT_RESTAURANT_FOOD_UPDATE_ORDER,$requestId,$dataDictionary,$connection);
            break;


        // 订单管理

        case EVENT_RESTAURANT_ORDER_FORM_LIST_FINISHED:
            // 已完成订单列表
            new \ActionsHttp\RestaurantOrderFormListFinished(EVENT_RESTAURANT_ORDER_FORM_LIST_FINISHED,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_ORDER_FORM_LIST_RETREAT_FINISHED:
            // 已退款订单列表
            new \ActionsHttp\RestaurantOrderFormListRetreatFinished(EVENT_RESTAURANT_ORDER_FORM_LIST_RETREAT_FINISHED,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_ORDER_FORM_LIST_RETREAT:
            // 退单申请列表
            new \ActionsHttp\RestaurantOrderFormListRetreat(EVENT_RESTAURANT_ORDER_FORM_LIST_RETREAT,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_ORDER_FORM_LIST_REMINDER:
            // 催单申请列表
            new \ActionsHttp\RestaurantOrderFormListReminder(EVENT_RESTAURANT_ORDER_FORM_LIST_REMINDER,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_ORDER_FORM_FOOD_LIST_RETREAT:
            // 退菜申请列表
            new \ActionsHttp\RestaurantOrderFormFoodListRetreat(EVENT_RESTAURANT_ORDER_FORM_FOOD_LIST_RETREAT,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_RETREAT_ORDER_FORM_CONFIRM:
            // 商家确认退单
            new \ActionsHttp\RestaurantRetreatOrderFormConfirm(EVENT_RESTAURANT_RETREAT_ORDER_FORM_CONFIRM,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_RETREAT_FOOD_CONFIRM:
            // 商家确认退菜
            new \ActionsHttp\RestaurantRetreatFoodConfirm(EVENT_RESTAURANT_RETREAT_FOOD_CONFIRM,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_REMINDER_CONFIRM:
            // 商家确认催单
            new \ActionsHttp\RestaurantReminderConfirm(EVENT_RESTAURANT_REMINDER_CONFIRM,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_PRINT:
            // 商家手动打印订单
            new \ActionsHttp\RestaurantPrint(EVENT_RESTAURANT_PRINT,$requestId,$dataDictionary,$connection);
            break;


        // 门店运营
        case EVENT_RESTAURANT_NOW_STATISTICS:
            new \ActionsHttp\RestaurantNowStatistics(EVENT_RESTAURANT_NOW_STATISTICS,$requestId,$dataDictionary,$connection);
            break;
        // 经营数据
        case EVENT_RESTAURANT_STATISTICS:
            // 数据统计
            new \ActionsHttp\RestaurantStatistics(EVENT_RESTAURANT_STATISTICS,$requestId,$dataDictionary,$connection);
            break;
        // 营业统计
        case EVENT_RESTAURANT_STATISTICS_BUSINESS:
            // 数据统计
            new \ActionsHttp\RestaurantStatisticsBusiness(EVENT_RESTAURANT_STATISTICS_BUSINESS,$requestId,$dataDictionary,$connection);
            break;
        // 账户对帐
        case EVENT_RESTAURANT_ACCOUNT_INFO:
            // 余额获取
            new \ActionsHttp\RestaurantAccountInfo(EVENT_RESTAURANT_ACCOUNT_INFO,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_WITHDRAWS:
            // 提现申请
            new \ActionsHttp\RestaurantWithdraws(EVENT_RESTAURANT_WITHDRAWS,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_WITHDRAWS_LIST_SUC:
            // 已提现列表
            new \ActionsHttp\RestaurantWithdrawsListSuc(EVENT_RESTAURANT_WITHDRAWS_LIST_SUC,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_WITHDRAWS_LIST_ING:
            // 正在提现列表
            new \ActionsHttp\RestaurantWithdrawsListIng(EVENT_RESTAURANT_WITHDRAWS_LIST_ING,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_ENTRY_LIST_YES:
            // 已入帐列表
            new \ActionsHttp\RestaurantEntryListYes(EVENT_RESTAURANT_ENTRY_LIST_YES,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_RESTAURANT_ENTRY_LIST_NOT:
            // 已入帐列表
            new \ActionsHttp\RestaurantEntryListNot(EVENT_RESTAURANT_ENTRY_LIST_NOT,$requestId,$dataDictionary,$connection);
            break;



            // 意见反馈
        case EVENT_RESTAURANT_SUGGESTION_TO:
            // 意见反馈
            new \ActionsHttp\RestaurantSuggestionTo(EVENT_RESTAURANT_SUGGESTION_TO,$requestId,$dataDictionary,$connection);
            break;



        /******************* 顾客 **********************/

        case EVENT_CONSUMER_LOGIN:
            // 顾客登录
            new \ActionsHttp\ConsumerLogin(EVENT_CONSUMER_LOGIN,$requestId,$dataDictionary,$connection,0);
            break;
        case EVENT_CONSUMER_GET_RESTAURANT_INFO:
            // 获取商铺信息
            new \ActionsHttp\ConsumerGetRestaurantInfo(EVENT_CONSUMER_GET_RESTAURANT_INFO,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_CONSUMER_GET_FOOD_LIST:
            // 获取商铺商品列表
            new \ActionsHttp\ConsumerGetFoodList(EVENT_CONSUMER_GET_FOOD_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_CONSUMER_ORDER_NEW:
            // 顾客下单
            new \ActionsHttp\ConsumerOrderNew(EVENT_CONSUMER_ORDER_NEW,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_CONSUMER_REMINDER:
            // 顾客催单
            new \ActionsHttp\ConsumerReminder(EVENT_CONSUMER_REMINDER,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_CONSUMER_RETREAT_FOOD:
            // 顾客退菜
            new \ActionsHttp\ConsumerRetreatFood(EVENT_CONSUMER_RETREAT_FOOD,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_CONSUMER_RETREAT_ORDER_FORM:
            // 顾客退单
            new \ActionsHttp\ConsumerRetreatOrderForm(EVENT_CONSUMER_RETREAT_ORDER_FORM,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_CONSUMER_ORDER_FINISHED_LIST:
            // 顾客订单列表
            new \ActionsHttp\ConsumerOrderFinishedList(EVENT_CONSUMER_ORDER_FINISHED_LIST,$requestId,$dataDictionary,$connection);
            break;
        case EVENT_CONSUMER_ORDER_INFO:
            // 顾客单个订单信息
            new \ActionsHttp\ConsumerOrderInfo(EVENT_CONSUMER_ORDER_INFO,$requestId,$dataDictionary,$connection);
            break;

        default:
            $connection->send(\Actions\ActionUtils::getResponseData(array(),$data_arr[SYS_EVENT],$requestId,RESULT_CODE_NOT_FOUND,"没有找到该通讯"));
            break;

    }
};

// 运行worker
Worker::runAll();